package com.bw.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.bw.entity.User;
import com.bw.entity.UserQuery;
import com.bw.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    UserService userService;


//    @RequestMapping("/pic.do") // 定义访问路径
//    public void pic(HttpServletResponse response) throws IOException {
//        // 自定义纯数字的验证码（随机4位数字，可重复）
//        String numbers = RandomUtil.randomNumbers(4);
//        RandomGenerator randomGenerator = new RandomGenerator(numbers, 4);
//        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
//        lineCaptcha.setGenerator(randomGenerator);
//        // 重新生成code
//        lineCaptcha.createCode();
//        lineCaptcha.write(response.getOutputStream());
//
//    }

    //    @RequestMapping(value = "/list.do",method = RequestMethod.GET)
    @GetMapping("/list.do")
//    @PostMapping
    public String list(UserQuery query, Integer pageNum, Model model) {
        // 开启分页， 第几页， 每页多少条数据
        PageHelper.startPage(pageNum == null ? 1 : pageNum, 3);
        // 执行分页查询
        List<User> userList = userService.selectByFuzzy(query);
        // 包装查询结果
        PageInfo<User> pageInfo = new PageInfo<>(userList);

        model.addAttribute("pageInfo", pageInfo);

        // 查询条件回显
        model.addAttribute("query", query);
        return "list";
    }


    @GetMapping("/list2.do")
    @ResponseBody  // 把返回值 序列化为Json格式的字符串
    public List<User> list2() {
        List<User> userList = userService.selectAll();

        return userList;
    }

    /**
     * 返回一个登录页面
     *
     * @return
     */
    @RequestMapping("/loginPage.do")
    public String loginPage() {
        return "login";
    }

    /**
     * 执行登录
     *
     * @return
     */
    @RequestMapping("/login.do")
    public String login(User user, Model model, HttpServletRequest request) {
        // 1.非空验证
        if (StrUtil.isBlank(user.getUsername())) {
            model.addAttribute("error", "用户名不能为空");
            return "login";
        }
        if (StrUtil.isBlank(user.getPassword())) {
            model.addAttribute("error", "密码不能为空");
            return "login";
        }

        // 2. 用户名不存在
        User userFromDB = userService.selectByName(user.getUsername());
        if (userFromDB == null) {
            model.addAttribute("error", "用户名不存在");
            return "login";
        }
        // 3. 密码错误
        String dbPassword = userFromDB.getPassword();
        String password = user.getPassword();

        if (!password.equals(dbPassword)) {
            model.addAttribute("error", "密码错误");
            return "login";
        }
        // 4. 把用户放入 session
        request.getSession().setAttribute("currentUser", userFromDB);

        // 重定向到list.do 方法     告诉浏览器 接下来去访问 某一个地址 （list.do）
        return "redirect:list.do";
//        return "list";
    }


}
